//
//  AppDelegate.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "AppDelegate.h"
#import "TabBarController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

@synthesize window = _window;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = UIColor.whiteColor;
    [self checkUpdateData];
    if (ISLOGIN) {
        [self requestData];
        [self gotoApp];
    }else{
        [self gotoLogin];
    }
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)gotoApp
{
    TabBarController *tabbarController = [[TabBarController alloc]init];
    self.window.rootViewController = tabbarController;
}

- (void)gotoLogin
{
    id loginVC = [[NSClassFromString(@"LoginVC") alloc]init];
    UINavigationController * nav = [[BaseNavController alloc]initWithRootViewController:loginVC];
    self.window.rootViewController = nav;
}

-(void)checkUpdateData{
    
    [SPNetworkingIns GET:PORT(@"bh-center/api/invite/appInfo") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        NSInteger type = [K_TimeJson(responseObject, @"data") integerValue];
        [[NSUserDefaults standardUserDefaults] setBool:type == 0 ? NO : YES forKey:@"isUpdate"];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(void)requestData{
    
    if (ISLOGIN) {
        [SPNetworkingIns GET:PORT(@"user-center/api/appUser/getAppUserInfo") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
           UserInfoModel * model = [UserInfoModel yy_modelWithDictionary:responseObject[@"data"]];
           [UserInfoModel shareInstance].userModel = model;
            if (model.isBind) {
                [[NSNotificationCenter defaultCenter] postNotificationName:DeviceConnectionNotification object:nil];
                [[BCLRingManager shared] connectDeviceWithMacAddress:model.mac timeout:10 completion:^(BOOL success, NSString * _Nullable deviceName, NSString * _Nullable error) {
                    if (!success) {
                        NSDictionary * info = @{@"state":@(NO)};
                        [[NSNotificationCenter defaultCenter] postNotificationName:BatteryLevelNotification object:info];
                    }
                }];
            }
        } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
        }];
    }
}

@end
